import os
from datetime import datetime
from common.c_public import getStrLine
import pluggy
import pytest

# from py._xmlgen import html
from py.xml import html


@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):

    out = yield

    report = out.get_result()
    if report.when == 'call' and report.failed:
        mode = 'a' if os.path.exists('failures') else 'w'
        try:  # Just to not crash py.test reporting
            pass  # the test 'item' failed

        except Exception as e:
            pass



    global  description,uri
    description = str(item.function.__doc__)
    # print('description:%s' % description)
    uri = getStrLine(description,"uri")
    uri = None

    global runResult
    runResult = report.outcome

@pytest.mark.optionalhook
def pytest_html_results_summary(prefix, summary, postfix):
    prefix.extend([html.p("测试人: 超好播测试组")])

@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
    cells.insert(1, html.th('description'))
    cells.insert(2, html.th('Test'))  #增加一列，列名 Test，解决用例参数入参为中文时正常显示
    cells.insert(3, html.th('URI', class_='sortable time', col='time'))
    cells.insert(4, html.th('Time', class_='sortable time', col='time'))
    cells.pop(5) #将原有的test删除。
    cells.pop(6)

@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
    # cells.insert(1, html.td(description))
    cells.insert(1, html.td("接口测试第一层"))
    # cells.insert(2, html.td(report.nodeid))             # report.nodeid 节点名称
    cells.insert(2, html.td(report.nodeid))
    cells.insert(3,html.td(uri=None))
    cells.insert(4, html.td(datetime.now().__str__()[0:19], class_='col-time'))
    cells.pop(5)
    cells.pop(6)


@pytest.fixture(scope="session",autouse=True)                     # scope='session'
def init_driver():

    yield

